package com.aya.transaction.service;

import com.aya.transaction.entity.Blog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BlogRequiredNewTransactionService {
    @Resource
    BlogSimpleTransactionService blogSimpleTransactionService;

    @Transactional
    public void updateByIdSuccess(String id, String content, String id2, String content2) {
        blogSimpleTransactionService.updateById(id, content);
        blogSimpleTransactionService.updateByIdRequiredNew(id2, content2);
    }

    @Transactional
    public void updateByIdCatchFailure(String id, String content, String id2, String content2) {
        blogSimpleTransactionService.updateById(id, content);
        try {
            blogSimpleTransactionService.updateByIdRequiredNewFail(id2, content2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Transactional
    public void updateByIdInnerFailure(String id, String content, String id2, String content2) {
        Blog blog = blogSimpleTransactionService.selectByPrimaryKey(id);
        System.out.println("parent blog:"+blog);
        blogSimpleTransactionService.updateById(id, content);
        try {
            blogSimpleTransactionService.updateByIdRequiredNewFail(id2, content2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            blogSimpleTransactionService.updateByIdRequiredNewFail(id2, content2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Transactional
    public void updateByIdOuterFailure(String id, String content, String id2, String content2) {
        blogSimpleTransactionService.updateById(id, content);
        blogSimpleTransactionService.updateByIdRequiredNew(id2, content2);
        throw new RuntimeException("exception");
    }

}
